﻿// 功能：蓝牙控制类
// 描述：
// 作者：温伟鹏
// 日期：2010/9/15 9:58:48

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace MeizuSDK.Core
{
    /// <summary>
    /// 蓝牙控制类
    /// </summary>
    public static class Bluetooth
    {
        #region DllImport
        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool SettingApiResetBluetoothSettings();

        //[DllImport("PlatformAPIWrapper.dll")]
        //private static extern bool SettingApiGetBTAddress(ref string pwcsBTAffr);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern int SettingApiSwitchBTPower(bool bPowerOn);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool SettingApiIsBluetoothAvailable();

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool SettingApiBTUpdateName(string pName);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool SettingApiBTIsOn();
        #endregion

        #region 私有变量
        /// <summary>
        /// 蓝牙MAC地址
        /// </summary>
        private static string btAddress;

        #endregion

        #region 公共属性
        /// <summary>
        /// 获取一个值，指示蓝牙设备是否已经开启
        /// </summary>
        public static bool IsPowerOn
        {
            get
            {
                return SettingApiBTIsOn();
            }
        }
        /// <summary>
        /// 获取一个值，指示本机是否存在蓝牙设备。
        /// </summary>
        public static bool IsBluetoothAvailable
        {
            get
            {
                return SettingApiIsBluetoothAvailable();
            }
        }
        /// <summary>
        /// 获取蓝牙地址字符串
        /// </summary>
        public static string Address
        {
            get
            {
                if (String.IsNullOrEmpty(btAddress))
                {
                    RegistryKey lmKey = Registry.LocalMachine;

                    RegistryKey bluetoothKey = lmKey.OpenSubKey(@"\Software\Microsoft\Bluetooth\Meizu");

                    string address = Convert.ToString(bluetoothKey.GetValue("HardwareBluetoothAddress", "000000000000"));

                    bluetoothKey.Close();

                    lmKey.Close();

                    if (address.Length == 12)
                    {
                        for (int i = 0; i < 6; i++)
                        {
                            if (i != 0)
                                btAddress += ":" + address.Substring(i * 2, 2);
                            else
                                btAddress += address.Substring(i * 2, 2);
                        }
                    }
                    else
                    {
                        btAddress = "00:00:00:00:00:00";
                    }
                }
                return btAddress;
            }
        }
        #endregion

        #region 函数
        /// <summary>
        /// 打开蓝牙设备
        /// </summary>
        /// <returns>返回true，表示成功打开蓝牙；否则表示打开失败</returns>
        public static bool OpenBluetooth()
        {
            int result = SettingApiSwitchBTPower(true);

            return result > 0;
        }
        /// <summary>
        /// 关闭蓝牙设备
        /// </summary>
        public static void CloseBluetooth()
        {
            int result = SettingApiSwitchBTPower(false);
        }
        /// <summary>
        /// 更新蓝牙设备名称
        /// </summary>
        /// <param name="name">蓝牙设备名称</param>
        /// <returns>更新成功返回true；否则返回false</returns>
        public static bool UpdateBooluetoothName(string name)
        {
            bool result = false;

            try
            {
                result = SettingApiBTUpdateName(name);
            }
            catch (Exception) { }

            return result;
        }
        #endregion
    }
}
